package com.orion.object;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;

/**
 * 自定义的编码器
 *
 * @author li.lc
 */
public class MyClientHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        try {
            User user = (User) msg;
            System.out.println(System.currentTimeMillis() + "---收到服务端发来的数据：" + user.toString());
        } finally {
            ReferenceCountUtil.release(msg);
        }
    }

    /**
     1644588159920---编码器ObjToJson ==> User:User(id=11, name=小红, age=23, birthDay=2022-02-11T22:02:39.809),对应json：{"birthDay":1644588159809,"name":"小红","id":11,"age":23}
     1644588160082---{"birthDay":1644588159809,"name":"小红","id":11,"age":23}
     1644588160112---收到服务端发来的数据：User(id=11, name=小红, age=23, birthDay=2022-02-11T22:02:39.809)
     */

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        // Close the connection when an exception is raised.
        System.out.println("连接发生异常：" + cause.getMessage());
        ctx.close();
    }
}
